package com.proj.hot.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.proj.hot.GlobalParams;
import com.proj.hot.R;
import com.proj.hot.activity.LoginActivity;
import com.proj.hot.activity.VODBuyPlayActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物
 * Created by zangchenggang on 17/6/30.
 */
public class ShoppingFragment extends BaseFragment {

    private List<View> viewList = new ArrayList<>();
    private View shop_view1, shop_view2, shop_view3, shop_view4, shop_view5, shop_view6, shop_view7, shop_view8,shop_view9;
    private ImageView shop_poster1, shop_poster2, shop_poster3, shop_poster4, shop_poster5, shop_poster6, shop_poster7, shop_poster8,shop_poster9;
    JSONArray dataArray;
    String code;

    private TextView text1, text2, text3, text4, text5, text6, text7, text8, text9;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                setView();

            }
        }
    };


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (code != null) {
            getData();
        }
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_shopping, container, false);
        shop_view1 = rootView.findViewById(R.id.shop_rl_poster1);
        shop_view2 = rootView.findViewById(R.id.shop_rl_poster2);
        shop_view3 = rootView.findViewById(R.id.shop_rl_poster3);
        shop_view4 = rootView.findViewById(R.id.shop_rl_poster4);
        shop_view5 = rootView.findViewById(R.id.shop_rl_poster5);
        shop_view6 = rootView.findViewById(R.id.shop_rl_poster6);
        shop_view7 = rootView.findViewById(R.id.shop_rl_poster7);
        shop_view8 = rootView.findViewById(R.id.shop_rl_poster8);
        shop_view9 = rootView.findViewById(R.id.shop_rl_poster9);
        viewList.add(shop_view1);
        viewList.add(shop_view2);
        viewList.add(shop_view3);
        viewList.add(shop_view4);
        viewList.add(shop_view5);
        viewList.add(shop_view6);
        viewList.add(shop_view7);
        viewList.add(shop_view8);
        viewList.add(shop_view9);

        shop_poster1 = (ImageView) rootView.findViewById(R.id.shop_poster1);
        shop_poster2 = (ImageView) rootView.findViewById(R.id.shop_poster2);
        shop_poster3 = (ImageView) rootView.findViewById(R.id.shop_poster3);
        shop_poster4 = (ImageView) rootView.findViewById(R.id.shop_poster4);
        shop_poster5 = (ImageView) rootView.findViewById(R.id.shop_poster5);
        shop_poster6 = (ImageView) rootView.findViewById(R.id.shop_poster6);
        shop_poster7 = (ImageView) rootView.findViewById(R.id.shop_poster7);
        shop_poster8 = (ImageView) rootView.findViewById(R.id.shop_poster8);
        shop_poster9 = (ImageView) rootView.findViewById(R.id.shop_poster9);

        text1=(TextView) rootView.findViewById(R.id.shop_text1);
        text2=(TextView) rootView.findViewById(R.id.shop_text2);
        text3=(TextView) rootView.findViewById(R.id.shop_text3);
        text4=(TextView) rootView.findViewById(R.id.shop_text4);
        text5=(TextView) rootView.findViewById(R.id.shop_text5);
        text6=(TextView) rootView.findViewById(R.id.shop_text6);
        text7=(TextView) rootView.findViewById(R.id.shop_text7);
        text8=(TextView) rootView.findViewById(R.id.shop_text8);
        text9=(TextView) rootView.findViewById(R.id.shop_text9);


        setOnListener();
        return rootView;
    }

    private void setOnListener() {

        for (int i = 0; i < viewList.size(); i++) {
            final int k = i;
            viewList.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // if jsonarray type=13
                    if (GlobalParams.IS_LOGIN) {
                        Intent intent = new Intent(getActivity().getApplicationContext(), VODBuyPlayActivity.class);
                        try {
                            intent.putExtra("contentid", dataArray.getJSONObject(k).getString("recode"));
                            intent.putExtra("goodsORtrave", "goods");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getActivity().getApplicationContext(), LoginActivity.class);
                        startActivity(intent);
                    }
                }
            });
        }
    }


    public void setCodeAndInitData(String code) {

        this.code = code;

    }


    private void getData() {

        try {


            /**
             * ---------------body------------------
             */
            JSONObject body = new JSONObject();
            body.put("code", code);
            body.put("sn", GlobalParams.SN);

            RequestParams params = new RequestParams(GlobalParams.reqHost + "featuredFirst/getDetails?"+code);
            params.setAsJsonContent(true);
            params.setBodyContent(body.toString().trim());

            /**
             * ---------------send------------------
             */
            x.http().post(params, new org.xutils.common.Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    try {
                        JSONObject jData = new JSONObject(s);

                        JSONObject respbody = jData.getJSONObject("respbody");
                        dataArray = respbody.getJSONArray("data");
                        handler.sendEmptyMessage(0);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onFinished() {

                }

                @Override
                public void onCancelled(CancelledException e) {

                }

                @Override
                public void onError(Throwable throwable, boolean b) {

                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    void setView() {

        try {
            ImageLoader.getInstance().displayImage(dataArray.getJSONObject(0).getString("logo"), shop_poster1);
            ImageLoader.getInstance().displayImage(dataArray.getJSONObject(1).getString("logo"), shop_poster2);
            ImageLoader.getInstance().displayImage(dataArray.getJSONObject(2).getString("logo"), shop_poster3);
            ImageLoader.getInstance().displayImage(dataArray.getJSONObject(3).getString("logo"), shop_poster4);
            ImageLoader.getInstance().displayImage(dataArray.getJSONObject(4).getString("logo"), shop_poster5);
            ImageLoader.getInstance().displayImage(dataArray.getJSONObject(5).getString("logo"), shop_poster6);
            ImageLoader.getInstance().displayImage(dataArray.getJSONObject(6).getString("logo"), shop_poster7);
            ImageLoader.getInstance().displayImage(dataArray.getJSONObject(7).getString("logo"), shop_poster8);
            ImageLoader.getInstance().displayImage(dataArray.getJSONObject(8).getString("logo"), shop_poster9);

            if (GlobalParams.screenHeight == 1080) {//1920*1080 dpi=160
                text1.setTextSize(18);
                text2.setTextSize(18);
                text3.setTextSize(18);
                text4.setTextSize(18);
                text5.setTextSize(18);
                text6.setTextSize(18);
                text7.setTextSize(18);
                text8.setTextSize(18);
                text9.setTextSize(18);

            }

            text1.setText(dataArray.getJSONObject(0).getString("name"));
            text2.setText(dataArray.getJSONObject(1).getString("name"));
            text3.setText(dataArray.getJSONObject(2).getString("name"));
            text4.setText(dataArray.getJSONObject(3).getString("name"));
            text5.setText(dataArray.getJSONObject(4).getString("name"));
            text6.setText(dataArray.getJSONObject(5).getString("name"));
            text7.setText(dataArray.getJSONObject(6).getString("name"));
            text8.setText(dataArray.getJSONObject(7).getString("name"));
            text9.setText(dataArray.getJSONObject(8).getString("name"));
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }


}
